package com.d5xx.hunters.commands;

import com.d5xx.hunters.API.ConsoleColor;
import com.d5xx.hunters.API.Hunter;
import com.d5xx.hunters.API.HunterPlayer;
import com.d5xx.hunters.Functions;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
import org.bukkit.entity.Player;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

import static com.d5xx.hunters.Variables.*;

@SuppressWarnings("NullableProblems")
public class HunterAdmin implements TabExecutor {
    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String a, String[] args) {
        if (args.length == 2) {
            if (args[0].equalsIgnoreCase("delete")) {
                World w = Bukkit.getWorld(args[1]);
                if (w != null) {
                    worldList.remove(w);
                    toDeleteWorldLsit.add(w);
                } else {
                    if (sender instanceof Player) {

                    } else logger.severe(ConsoleColor.red() + "Could not find world " + args[1] + ConsoleColor.reset());
                }
            } else if (args[0].equalsIgnoreCase("unload")) {
                Bukkit.unloadWorld(Objects.requireNonNull(Bukkit.getWorld(args[1])), false);
            }
        }else if (args.length == 3) {
            if (args[0].equalsIgnoreCase("move")) {
                World w = Bukkit.getWorld(args[2]);
                if (w != null) {
                    Objects.requireNonNull(Bukkit.getPlayer(args[1])).teleport(Objects.requireNonNull(Bukkit.getWorld(args[2])).getSpawnLocation());
                } else {
                    if (sender instanceof Player) {
                        Hunter.getHunterPlayer((Player) sender).sendKeyMessage("cmd-admin-move", true);
                    } else logger.severe(ConsoleColor.red() + "Could not find world " + args[2] + ConsoleColor.reset());
                }
            }

        }
        if (!(sender instanceof Player player)) return true;
        HunterPlayer hunterPlayer = Hunter.getHunterPlayer(player);
        if (args.length == 0) { //子命令长度为0
            hunterPlayer.sendKeyMessage("HunterAdmin-help", false);
        } else if (args.length == 1) {
            switch (args[0]) {
                case "help":
                    hunterPlayer.sendKeyMessage("HunterAdmin-help", false);
                    break;
                case "setlobby":
                    Functions.setLobby(player.getLocation());
                    hunterPlayer.sendKeyMessage("HunterAdmin-setlobby", true);
                    break;
                default:
            }
        }
        return true;
    }

    @Override
    public List<String> onTabComplete(CommandSender sender, Command cmd, String a, String[] args) {
        //Tab补全
        if (args.length == 1) {
            List<String> result = new ArrayList<>(Arrays.asList("help", "setlobby", "delete", "unload"));
            result.removeIf(s -> !s.startsWith(args[0].toLowerCase()));
            return result;
        }
        return null;
    }
}
